from datetime import datetime
from flask import Flask, render_template, session, redirect, url_for, flash
from . import main
from .forms import UserForm
from ..models import User
from .. import db


#  路由修饰器由蓝本提供
@main.route('/', methods=['GET', 'POST'])
def index():
    form = UserForm()
    if form.validate_on_submit():
        old_name = session.get('name')
        if old_name is not None and old_name != form.name.data:
            flash('Looks like you have changed your name!')
        session['name'] = form.name.data
        #  url_for('.index')= url_for('main.index')
        return redirect(url_for('.index'))
    return render_template('index.html', form=form, name=session.get('name'))